import axios from 'axios'
import { Message,MessageBox } from 'element-ui';

const https = axios.create({
    // baseURL: 'http://shiyansong.cn:8888/api/private/v1/',
  // baseURL:'https://lianghj.top:8888/api/private/v1/'
  baseURL: process.env.VUE_APP_API,  
})

// 添加请求拦截器
https.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    let token = localStorage.getItem('token')
    if (token) {
        config.headers.Authorization = token
    }
    return config;
  }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  });

// 添加响应拦截器
https.interceptors.response.use(function (res) {
    let msg = res.data.meta.msg
    let status = res.data.meta.status

    if (msg == '无效token') {
        MessageBox.confirm('token过期是否留在本页', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            return false
        }).catch(() => {
            localStorage.removeItem('token')
            location.href = '/login'
        })
    }

    if (status === 200 || status === 201) {
        Message({
          message: msg,
          type: 'success'
        });
    }else if (status === 400 || status === 401 || status === 403 || status === 404 || status === 500) {
        Message({
          message: msg,
          type: 'error'
        });
    }   

    // 对响应数据做点什么
    return res;
  }, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
  });



export default https